<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Uinfo_mdl extends MY_Model {

    public function __construct() {
        parent::__construct();
        $this->table = 'users_info';
    }

    /**
     * 扣除用户余额
     * @param  $money 需要扣除的余额(注意为正数)
     * @param  $mess  消费记录表消息记录
     * @param  $uid   需要扣除的用户ID，默认为当前用户
     * @return 布尔值
     */
    public function debit($money,$mess,$uid=''){
        if(empty($uid)){
            $uid = $_SESSION['user']['id'];
        }
        if($money < 0){
            return false;
        }
        $this->load->model('bill_mdl');
        $data_bill = array(
            'user_id' => $uid,
            'charges' => $money*-1,
            'remarks' => $mess,
            'update_time' => $_SERVER['REQUEST_TIME']
        );
        $r = $this->bill_mdl->create($data_bill);
        if($r){
            $this->db->set('money','money-'.$money,FALSE);
            $this->db->where('id',$uid);
            return $this->update();
        }else{
            return false;
        }
    }

}